define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virborrow/index',
                    add_url: 'virborrow/add',
                    edit_url: 'virborrow/edit',
                    del_url: 'virborrow/del',
                    multi_url: 'virborrow/multi',
                    table: 'virtual_borrow',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'borrowName', title: __('BorrowName')},
                        {field: 'apr', title: __('Apr')},
                        {field: 'interest', title: __('Interest')},
                        {field: 'addTime', title: __('AddTime'), formatter: Table.api.formatter.datetime},
                        {field: 'fullTime', title: __('FullTime'), formatter: Table.api.formatter.datetime},
                        {field: 'repayTime', title: __('RepayTime'), formatter: Table.api.formatter.datetime},
                        {field: 'account', title: __('Account')},
                        {field: 'raiseAccount', title: __('RaiseAccount')},
                        {field: 'money', title: __('Money')},
                        {field: 'time', title: __('Time'), formatter: Controller.api.formatter.time},
                        //{field: 'borrowItem', title: __('BorrowItem')},
                        {field: 'validDay', title: __('ValidDay'), formatter: Controller.api.formatter.validDay},
                        {field: 'status', title: __('Status'), formatter: Controller.api.formatter.status},
                        //{field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter : {
                status : function(value,row,index){
                    if(value==0){
                        return "投标中";
                    }
                    if(value==1){
                        return "满标待还";
                    }
                    if(value==2){
                        return "还款中";
                    }
                    if(value==3){
                        return "已完成";
                    }
                },
                time : function(value){
                    return value+'天';
                },
                validDay : function(value){
                    return value+'天';
                }
            }
        }
    };
    return Controller;
});